﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Runtime.Caching;

namespace Data
{
    /// <summary>
    /// 内存缓存，依赖System.Runtime.Caching包
    /// </summary>
    public class CacheHelper
    {
        private static ObjectCache cache = MemoryCache.Default;

        public static void AddItem(string key, object value)
        {
            // 创建一个缓存策略，设置绝对过期时间为 永不过期
            CacheItemPolicy policy = new CacheItemPolicy();// { AbsoluteExpiration = DateTimeOffset.Now.AddYears(100) };
            cache.Set(key, value, policy);
        }

        public static object GetItem(string key)
        {
            return cache.Get(key);
        }

        public static void RemoveItem(string key)
        {
            cache.Remove(key);
        }


        public static void Test()
        {
            string key = "test";
            Object obj2 = new Object();
            object obj = CacheHelper.GetItem(key);
            CacheHelper.AddItem(key, obj2);
        }
    }

}